<!DOCTYPE html>
<html>
<head>   
    <title>application</title>   
    <script type="text/javascript" src="../javascript/page/Child.js"></script>
    <style type="text/css">
        html, body {
            font-size: 12px;  background-color: #f1f1f1;
        }
    </style>
</head>
<body onload="docLoaded();">
    <div id="queryForm" class="title_bar">
        <template>
            <div class="title_bar">
                <el-form :inline="true" :model="searchData" class="demo-form-inline">           
                    <el-form-item label="Application">
                        <el-input v-model="searchData.name" placeholder="Application name"></el-input>
                    </el-form-item>           
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">Query</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="handleAdd" icon="el-icon-plus" plain>Add</el-button>
                    </el-form-item>
                </el-form>
            </div>       
            <div class="hline"></div>
            <div class="panel">              
                <el-table :data="tableData" style="width: 100%" max-height="560" stripe= "true">
                    <el-table-column prop="id" label="AppID" min-width="5%"></el-table-column>
                    <el-table-column prop="name" label="Application" min-width="20%"></el-table-column>
                    <el-table-column prop="description" label="Description" min-width="30%"></el-table-column>
                    <el-table-column label="Status" min-width="10%">
                        <template slot-scope="scope">
                            <el-tag :type="scope.row.statusType">{{scope.row.statusName}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="Operation" min-width="35%">
                        <template slot-scope="scope">
                            <el-link type="text" :href="'./profiles.htm?appId='+scope.row.id+'&appName='+scope.row.name" underline="false">属性&nbsp;&nbsp;</el-link>
                            <el-button @click="handleEdit(scope.row)" type="text">编辑</el-button>
                            <el-button @click="handleCopy(scope.row)" type="text">复制</el-button>
                        </template>
                    </el-table-column>
                </el-table>                 
            </div>   
            <div class="status_bar" style="text-align: left;">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="searchData.pageNum"
                    :page-sizes="[8, 15, 20]"
                    :page-size="searchData.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="pageInfo.total">
                </el-pagination> 
            </div>
            <el-dialog :title="dialogTitle" :visible.sync="editDialogFormVisible"> 
                <el-form :model="dialogForm">                
                    <el-form-item label="Application" :label-width="formLabelWidth">
                        <el-input v-model="dialogForm.name" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="Description" :label-width="formLabelWidth">
                        <el-input v-model="dialogForm.description" autocomplete="off" type="textarea"></el-input>
                    </el-form-item>
                    <el-form-item label="Status" :label-width="formLabelWidth">
                        <el-select v-model="dialogForm.status" placeholder="please select status">
                            <el-option v-for="item in statusList"
                            :key="item.status"
                            :label="item.name"
                            :value="item.status">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="editDialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="editSubmit">确 定</el-button>
                </div>                
            </el-dialog>
        </template>
    </div>
    <script type="text/javascript">
        var router = startPage(); 
        function docLoaded() {
            var queryApp = new Vue({el: '#queryForm', data: {
                    searchData:{
                        name:"",
                        pageNum:1,
                        pageSize:8                       
                    },
                    pageInfo:{
                        totalPage:0,
                        total:0
                    },
                    tableData:[],
                    statusList:[{status:0,name:"normal"},{status:1,name:"disable"}],
                    formLabelWidth: '120px',
                    editDialogFormVisible:false,                    
                    dialogTitle: "Edit",
                    dialogMode: 0,
                    dialogForm:{
                        id:0,
                        name:"", 
                        description:"", 
                        status: 0
                    }        
                },
                methods: {
                    onSubmit:function(event){                                           
                        httpPost(apiConfigAppPage(this,router.authInfo,JSON.stringify(this.searchData))).success(function(res,context){ 
                            parseAndCheckResult(context,res);
                        }).then(function(status, res, context){context.$message.error(res);});
                    },
                    handleSizeChange(val) {                     
                        this.searchData.pageSize = val;
                        this.onSubmit();
                    },
                    handleCurrentChange(val) {
                        this.searchData.pageNum = val;
                        this.onSubmit();
                    },
                    handleClick(row){
                        console.log(row);
                    },
                    handleEdit(row){
                        this.dialogForm = {id:row.id, name:row.name, description:row.description, status:row.status};                       
                        this.dialogTitle = "Update Application";
                        this.dialogMode = 0;
                        this.editDialogFormVisible = true;
                    },     
                    handleCopy:function(row){
                        this.dialogForm = {copyId:row.id, name:"", description:"", status:0};      
                        this.dialogTitle = "Copy To New Application";
                        this.dialogMode = 2;
                        this.editDialogFormVisible = true;
                    },              
                    handleAdd(){
                        this.dialogForm = {id:0, name:"", description:"", status:0};      
                        this.dialogTitle = "Add New Application";
                        this.dialogMode = 1;
                        this.editDialogFormVisible = true;
                    },
                    editSubmit:function(event){
                        this.editDialogFormVisible = false;
                        var apiRequest = apiConfigAppUpdate(this,router.authInfo,JSON.stringify(this.dialogForm));
                        if(this.dialogMode==1){
                            apiRequest = apiConfigAppAdd(this,router.authInfo,JSON.stringify(this.dialogForm));
                        }
                        if(this.dialogMode==2){
                            apiRequest = apiConfigAppCopy(this,router.authInfo,JSON.stringify(this.dialogForm));
                        }
                        httpPost(apiRequest).success(function(res,context){                          
                            checkResult(context,res,false);
                            context.onSubmit();
                        }).then(function(status, res, context){checkResult(context,res,true);});
                    }                               
                }
            }); queryApp.onSubmit();//init end.
        } 

        function parseAndCheckResult(vApp, res){
            var resObj = JSON.parse(res); 
            if(resObj.code != SUCCESS_CODE){
                return false;
            }
            if(resObj.pageInfo){
                vApp.pageInfo.total = resObj.pageInfo.total;
                vApp.pageInfo.totalPage = resObj.pageInfo.totalPage;
            }
            if(resObj.data){
                var arrayObj = new Array();
                for(var item of resObj.data){
                    arrayObj.push({
                        id:item.id,
                        name:item.name, 
                        description:item.description, 
                        status:item.deleted,
                        statusName:(item.deleted==1)?"disable":"normal",
                        statusType:(item.deleted==1)?"info":"success"});
                }
                vApp.tableData = arrayObj;
            }
            return resObj;
        }

        function checkResult(vApp, res, isErr){            
            if(isErr){
                vApp.$message.error(res);
                return;
            }
            var resObj = JSON.parse(res); 
            if(resObj.code == SUCCESS_CODE){
                vApp.$message({type: 'success', message: resObj.message});
                return;
            }
            vApp.$message.error(resObj.message+","+resObj.code);
        }
    </script>
</body>
</html>
